package com.pospi.pospihold.ui.cash_ui;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.pospi.pospihold.App;
import com.pospi.pospihold.R;
import com.pospi.pospihold.adapter.TableAdapter;
import com.pospi.pospihold.dto.TableAeraBeen;
import com.pospi.pospihold.dto.TableDto;
import com.pospi.pospihold.http.LoadServer;
import com.pospi.pospihold.ui.base.BaseActivity;
import com.pospi.pospihold.util.Api;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by acer on 2018/6/12.
 */

public class TableActivity extends BaseActivity {
    @Bind(R.id.gridView)
    GridView gridView;
    @Bind(R.id.parent_table)
    LinearLayout parent_table;

    private List<TableDto> tablebeens = new ArrayList<>();

    private TableDto tablebeen;
    private TableAdapter tableAdapter;
    private EditText et_table;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_table);
        setTitle("选择桌号");
        ButterKnife.bind(this);
        init();


    }
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    close();
                    tableAdapter.notifyDataSetChanged();
                    break;
                case 2:
                    close();
                    new AlertDialog.Builder(TableActivity.this)
                            .setTitle("提示")
                            .setMessage("服务器异常")
                            .setPositiveButton("重试", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    getTable();
                                }
                            }).setNegativeButton("取消", null).show();
                    break;

            }
        }
    };
    public void init() {
        tableAdapter = new TableAdapter(this, tablebeens);
        gridView.setAdapter(tableAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(TableActivity.this, PointSelectActivity.class);
                intent.putExtra("data", tablebeens.get(i));
                startActivity(intent);
                finish();
            }
        });
        getTable();
    }
    private void getTable(){
        showDialog();
        RequestParams params = new RequestParams();
        //得到店铺的Id
        SharedPreferences preferences = getSharedPreferences("StoreMessage", Context.MODE_PRIVATE);
        String id = preferences.getString("Id", "");//得到店铺的ID
        params.put("shopNo", id);
        LoadServer.getConnect(this, Api.GET_TABLE, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Log.i("table", new String(bytes));
                try {
                    JSONObject object = new JSONObject(new String(bytes));
                    JSONObject jsonObject = object.getJSONObject("Value");
                    JSONArray array = jsonObject.getJSONArray("tableArray");
                    JSONArray arerJsonArray = jsonObject.getJSONArray("arerArray");
                    List<TableAeraBeen> aeraBeens = new Gson().fromJson(arerJsonArray.toString(), new TypeToken<List<TableAeraBeen>>() {
                    }.getType());

                    List<TableDto> tableDtos = new Gson().fromJson(array.toString(), new TypeToken<List<TableDto>>() {
                    }.getType());
                    Log.i("table", aeraBeens.size() + "---" + tableDtos.size());
                    handTable(aeraBeens, tableDtos);
                    Message message = Message.obtain();
                    message.what = 1;
                    handler.sendMessage(message);
                } catch (JSONException e) {
                    e.printStackTrace();
                    Message message = Message.obtain();
                    message.what = 2;
                    handler.sendMessage(message);
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Message message = Message.obtain();
                message.what = 2;
                handler.sendMessage(message);
            }
        });

    }

    private void handTable(List<TableAeraBeen> aeraBeens, List<TableDto> tableDtos) {
        for (TableAeraBeen aeraBeen : aeraBeens) {
            for (TableDto tableDto : tableDtos) {
                if (aeraBeen.getSid().equals(tableDto.getAreaSid())) {
                    tableDto.settArea(aeraBeen.getAreaName());
                }
            }
        }
        tablebeens.clear();
        tablebeens.addAll(tableDtos);
    }


}
